<!--
 * @Description: 03-击鼓传花游戏
 * @Autor: wangxin
 * @Date: 2020-06-10 08:52:31
 * @LastEditors: Seven
 * @LastEditTime: 2020-06-10 08:53:47
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="queue.js"></script>
    <script>
      function hotPotato(elementsList, num) {
        const queue = new Queue()
        const elimitatedList = [] // 被淘汰的数组

        for (let i = 0; i < elementsList.length; i++) {
          queue.enqueue(elementsList[i])
        }

        while (queue.size() > 1) {
          for (let i = 0; i < num; i++) {
            queue.enqueue(queue.dequeue()) // 移除开头元素再加到末尾，实现了循环队列
          }
          elimitatedList.push(queue.dequeue()) // 这个时候开头第一个原生就是被淘汰的元素
        }

        return {
          eliminated: elimitatedList,
          winner: queue.dequeue(),
        }
      }

      // 测试代码
      const lists = ['张三', '李四', '王五', '王鑫']
      const result = hotPotato(lists, 10)
      console.log(result)
    </script>
  </body>
</html>
